﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace HRDexCapstoneProject
{
    public partial class NestedMasterPage1 : System.Web.UI.MasterPage
    {
        string sqlc;
        SqlConnection conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True");
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            sqlc = "select ParentFodler from formFolder where FolderID = " + (string)(Session["pFolder"]);
            SqlDataAdapter cmd = new SqlDataAdapter(sqlc, conn);

            DataTable dtbl = new DataTable();
            cmd.Fill(dtbl);

            string pfold = dtbl.Rows[0][0].ToString();
            string hrdoc = (string)(Session["hrdoc"]);
            if (pfold == "1")
            {
                if (hrdoc == "1")
                {
                    Session["pFolder"] = pfold;
                }
            }
            else
            {
                Session["pFolder"] = pfold;
            }
        }
        void sqlexecute(string sqlcmd)
        {
            SqlCommand cmd = new SqlCommand(sqlcmd, conn);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                conn.Close();
            }

        }

        protected void btnNewFolder_Click(object sender, EventArgs e)
        {
            txtnfName.Visible = true;
            btnCreateFolder.Visible = true;
        }

        protected void btnCreateFolder_Click(object sender, EventArgs e)
        {
            if (txtnfName.Text.Length != 0)
            {
                sqlc = "INSERT INTO formFolder (FolderName, ParentFodler) " +
                "VALUES( " + txtnfName.Text +
                "," + SessionID() + ")";
                sqlexecute(sqlc);
            }
        }

        protected void btnrename_Click(object sender, EventArgs e)
        {
            btnrefolder.Visible = true;
            txtrename.Visible = true;
            btnrename.Visible = false;
        }

        protected void btnrefolder_Click(object sender, EventArgs e)
        {
            sqlc = "UPDATE formFolder " +
                "SET FolderName = '" + txtrename.Text + 
                "WHERE FolderID = " + lbFolder.SelectedValue;
            sqlexecute(sqlc);
            lbFolder.SelectedItem.Text = txtrename.Text;
        }

        protected void btnenter_Click(object sender, EventArgs e)
        {
            Session["pFolder"] = lbFolder.SelectedValue;
        }

        protected void btndelete_Click(object sender, EventArgs e)
        {
            sqlc = "DELETE FROM formFile WHERE FolderID = " + lbFolder.SelectedValue;
            sqlexecute(sqlc);
            sqlc = "DELETE FROM formFolder WHERE FolderID = " + lbFolder.SelectedValue;
            sqlexecute(sqlc);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (btncommonplace.Text == "HR Central Folder")
            {
                Session["pFolder"] = hrcp();
                Session["hrcp"] = hrdID();
                btncommonplace.Text = "Return to Main Folder";
            }
            else
            {
                Session["pFolder"] = hrdID();
                Session["hrcp"] = "12";
                btncommonplace.Text = "HR Central Folder";
            }

        }
        string SessionID()
        {
            return Session["EmployeeID"].ToString();
        }
        string hrcp()
        {
            return (string)(Session["hrcp"]);
        }
        string hrdID()
        {
            return (string)(Session["hrdoc"]);
        }

        protected void btnSearch_Click(object sender, ImageClickEventArgs e)
        {
            dsFolder.SelectCommand = "SELECT FolderID, FolderName FROM formFolder WHERE FolderName like '" + TextBox1.Text +"%'";
        }
    }
}